﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DLL;
using System.Data;
using System.Data.SqlClient;

namespace BLL
{
   public class NhomThietBiBLL
    {
       DataServer DB = new DataServer();
       public DataTable Select_ALL()
       {
           string sql = string.Format("sp_NhomThietBi_GetByAll");
           DataTable bang = DB.LayDuLieuProc(sql);
           return bang;
       }
       public bool Them(NhomThietBiDAL ntb)
       {
           string sql = string.Format("sp_NhomThietBi_Insert");
           string MaTB = DLL.CommonFunc.GetPrimaryKey("NhomThietBi");
           SqlParameter paramMNTB = new SqlParameter("@NhomID", MaTB);
           SqlParameter paramTNTB = new SqlParameter("@TenNhomID", ntb.TenNhomID);

           try
           {
               DB.CapNhatProc(sql, paramMNTB, paramTNTB);
           }
           catch
           {
               return false;
           }
           DLL.CommonFunc.UpdatePrimaryKey("NhomThietBi");
           return true;
       }
       public bool Sua(NhomThietBiDAL ntb)
       {
           string sql = string.Format("sp_NhomThietBi_Update");
           SqlParameter paramMNTB = new SqlParameter("@NhomID", ntb.NhomID);
           SqlParameter paramTNTB = new SqlParameter("@TenNhomID", ntb.TenNhomID);
           try
           {
               DB.CapNhatProc(sql, paramMNTB, paramTNTB);
           }
           catch
           {
               return false;
           }
           return true;
       }
       public int Xoa(string MaNTB)
       {
           int kq = 0;
           string sql = string.Format("sp_NhomThietBi_Delete");
           SqlParameter paramMTB = new SqlParameter("@NhomID", MaNTB);
           SqlParameter paramKQ = new SqlParameter("@KetQua", SqlDbType.Int);
          paramKQ.Direction=ParameterDirection.Output;
               DB.CapNhatProc(sql, paramMTB,paramKQ);
               kq = int.Parse(paramKQ.Value.ToString());   
           return kq;
       }
       public bool Kiemtra(string Ma)
       {
           string sql = string.Format("sp_NhomThietBi_Kiemtra");
           SqlParameter paramMNTB = new SqlParameter("@NhomID",Ma);
           DataTable bang = DB.LayDuLieuProc(sql,paramMNTB);
           if (bang.Rows.Count > 0)
           {
               return true;
           }
           return false;
       }
       public DataTable Search(NhomThietBiDAL ntb)
       {
           string sql = string.Format("sp_NhomThietBi_Search");
           SqlParameter paramMTB = new SqlParameter("@NhomID", ntb.NhomID);
           SqlParameter paramTTB = new SqlParameter("@TenNhomID", ntb.TenNhomID);    
           DataTable bang = DB.LayDuLieuProc(sql, paramMTB, paramTTB);
           return bang;
       }
       public bool KiemtraTen(string TenTB)
       {
           string sql = "select TenNhomID from NhomThietBi where  TenNhomID=@TenNhomID";
           SqlParameter paramMNTB = new SqlParameter("@TenNhomID", TenTB);
           DataTable bang = DB.LayDulieu(sql, paramMNTB);
           if (bang.Rows.Count > 0)
           {
               return true;
           }
           return false;
       }
    }
}
